package demo5.example;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @program: java-generic
 * @description: 下限通配符2
 * @author: bjc
 * @create: 2021-08-15 16:04
 */
public class Main3 {
    public static void main(String[] args) {
//        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator2());
        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator1());
        //报错
//        TreeSet<Cat> treeSet = new TreeSet<>(new Comparator3());

        treeSet.add(new Cat("jerry",20));
        treeSet.add(new Cat("amy",22));
        treeSet.add(new Cat("jim",15));
        for (Cat cat : treeSet) {
            System.out.println(cat);
        }
    }
}

class Comparator1 implements Comparator<Animal>{
    @Override
    public int compare(Animal o1, Animal o2) {
        return o1.name.compareTo(o2.name);
    }
}

class Comparator2 implements Comparator<Cat>{
    @Override
    public int compare(Cat o1, Cat o2) {
        return o1.age-o2.age;
    }
}

class Comparator3 implements Comparator<MiniCat>{
    @Override
    public int compare(MiniCat o1, MiniCat o2) {
        return o1.level-o2.level;
    }
}